<template>
    <div
      class="about-master-container"
      v-loading="loading"
    >
      <a-card class="mb20">
        <titleName title="危险源辨识" />
        <BForm
          ref="BForm"
          :config="formConfig"
          :form="form"
          :rule="rules"
        />
      </a-card>
      <a-card class="mb20">
        <titleName :title="typeTitle" />
        <a-form-model
          ref="form"
          :model="dangerMeetExtraDTO"
          :rules="rules"
        >
          <a-form-model-item
            :label="userTypeTitle"
            prop="content"
          >
            <BaseTextarea
              :disabled="resolveInfo"
              v-model="dangerMeetExtraDTO.content"
              :maxLength="3000"
            />
          </a-form-model-item>
        </a-form-model>
      </a-card>
      <div class="tool-btns page-btn-right-top">
        <a-button
          v-if="!resolveInfo"
          class="ml20"
          :loading="loading"
          type="primary"
          @click="save()"
        >提交</a-button>
        <a-button
          class="ml20"
          @click="$close"
        >关闭</a-button>
      </div>
    </div>
</template>
<script>
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 分公司
import BSelect from '@/components/BComponents/BSelect'
import SelectPerson from '@/components/Select/SelectPerson'
import SelectPersonByProject from '@/components/Select/SelectPersonByProject'
import api from '@/api/safetySupervision/base/constructionStage'
import seeSign from './seeSign'
import addModal from '@/views/safetySupervision/hazardIdentification/hazardIdentificationList/addModal'
import Api from '@/api/office/hazardIdentification'
import { finishBusinessTodo } from '@/api/commons'

let STATUS = {
  DRAFT: 0,
  WAIT_START: 1,
  STARTING: 2,
  SUBMITTING: 3,
  REJECT: 4,
  END: 5
}
export default {
  name: 'a' + Date.now(),
  components: {
    SelectSubCompany,
    SelectProject,
    SelectPerson,
    seeSign,
    addModal
  },
  computed: {
    // 是否是业务待办查看处理信息
    resolveInfo: {
      get() {
        return this.$route.query?.resolveInfo === 'true'
      },
      set() {}
    },
    /* eslint-disable */
    formConfig() {
      return [
        {
          row: true,
          children: [
            {
              type: SelectSubCompany,
              value: 'branchComId',
              label: '所属分公司',
              attrs: {
                disabled: true,
                autoSelected: true
              },
              on: {
                selectChange: (e) => (this.form.branchComName = e.orgName)
              },
              show: !this.form.id
            },
            {
              type: SelectProject,
              value: 'projectId',
              label: '项目名称',
              attrs: {
                form: this.form,
                disabled: true,
                keyValue: 'branchComId'
              },
              on: {
                selectChange: async (e) => {
                  this.form.projectName = e.name
                }
              },
              show: !this.form.id
            },
            {
              type: 'input',
              value: 'branchComName',
              label: '所属分公司',
              attrs: { disabled: true },
              show: this.form.id
            },
            {
              type: 'input',
              value: 'projectName',
              label: '项目名称',
              attrs: { disabled: true },
              show: this.form.id
            },
            {
              type: 'input',
              value: 'meetingName',
              label: '会议名称',
              attrs: { disabled: this.form.status !== STATUS.DRAFT }
            },
            {
              type: BSelect,
              value: 'identCycleTypeId',
              label: '辨识周期',
              attrs: {
                init: false,
                form: this.form,
                keyValue: 'projectId',
                disabled: true,
                request: async () => {
                  let res = await Api.chenkBaseIdent({}, { projectId: this.form.projectId })
                  if (res.code === this.SUCCESS_CODE) {
                    this.form.identCycleTypeId = res.data.id
                    return { code: this.SUCCESS_CODE, data: [res.data] }
                  }
                  return { code: this.SUCCESS_CODE, data: [] }
                }
              },
              show: this.form.id === null
            },
            {
              type: 'input',
              value: 'identCycleTypeStr',
              label: '辨识周期',
              attrs: { disabled: true },
              show: this.form.id !== null
            },
            // {
            //   type: 'input',
            //   value: 'projectName',
            //   label: '项目名称',
            //   attrs: { disabled: true },
            //   show: this.disabled
            // },
            {
              type: 'month-picker',
              value: 'month',
              label: '会议月份',
              attrs: {
                format: 'YYYY-MM',
                valueFormat: 'YYYY-MM',
                disabled: !this.canEdit
              }
            },
            {
              type: BSelect,
              value: 'cataDetailsId',
              label: '当前施工阶段',
              attrs: {
                keys: ['id', 'allName'],
                disabled: this.form.status !== STATUS.DRAFT,
                init: true,
                request: async () => {
                  let res = await api.list({}, { type: 0, haveTree: true })
                  if (res.code === this.SUCCESS_CODE) {
                    let temp = []
                    res.data.map((v) => {
                      temp = temp.concat(v.detailsVOS)
                    })
                    return { code: this.SUCCESS_CODE, data: temp.filter((v) => v.status === 0) }
                  }
                  return { code: this.SUCCESS_CODE, data: [] }
                }
              }
            },
            {
              type: 'textarea',
              value: 'meetingExplain',
              label: '会议说明',
              colSpan: 24,
              noFixed: true,
              attrs: { disabled: this.form.status !== STATUS.DRAFT }
            },
            // {
            //   type: ASelectOrgPeople, value: 'speaker', label: '主持人/主讲人',
            //   attrs: {
            //     disabled: this.disabled,
            //     selectPeopleName: this.form.speakerName,
            //     init: false,
            //     form: this.form,
            //     keyValue: 'projectId',
            //   },
            //   on: {
            //     selectPeopleNameDone: $event => {
            //       this.form.speakerName = $event.name
            //     },
            //     onSelectChange: $event => {
            //       this.form.speakerName = $event.name
            //     }
            //   }
            // },
            {
              type: SelectPersonByProject,
              value: 'speakerId',
              label: '主持人/主讲人',
              attrs: {
                showSearch: true,
                mode: 'combobox',
                'option-label-prop': 'children',
                disabled: this.form.status !== STATUS.DRAFT,
                projectId: this.form.projectId
              }
            },
            {
              type: 'date-picker',
              value: 'meetTime',
              label: '会议时间',
              attrs: {
                showTime: { format: 'HH:mm' },
                format: 'YYYY-MM-DD HH:mm',
                valueFormat: 'YYYY-MM-DD HH:mm:ss',
                disabled: this.form.status !== STATUS.DRAFT
              }
            },
            {
              type: 'input',
              value: 'meetingPlace',
              label: '会议地点',
              attrs: { disabled: this.form.status !== STATUS.DRAFT }
            },
            {
              type: BSelect,
              value: 'status',
              label: '状态',
              show: this.form.id,
              attrs: {
                disabled: true,
                list: [
                  { id: 0, name: '草稿' },
                  { id: 1, name: '待开始' },
                  { id: 2, name: '进行中' },
                  { id: 3, name: '审批中' },
                  { id: 4, name: '审批不通过' },
                  { id: 5, name: '完结' }
                ]
              }
            }
          ]
        }
      ]
    },
    typeTitle() {
      switch (this.noticeType) {
        case 201:
          return '下月施工进度安排'
        case 202:
          return '本月分包分供商合同和付款情况'
        case 203:
          return '本月检查发现的主要安全隐患'
        case 204:
          return '下月施工进度安排'
      }
      return '下月施工进度安排'
    },
    userTypeTitle() {
      switch (this.noticeType) {
        case 201:
          return '项目生产经理'
        case 202:
          return '项目商务经理'
        case 203:
          return '项目安全总监'
        case 204:
          return '项目总工'
      }
      return '项目生产经理'
    }
  },
  data() {
    return {
      STATUS,
      activeKey: 1,
      loading: false,

      disabled: true,
      noticeType: -1,

      form: {
        id: '',
        status: STATUS.DRAFT,

        meetingEmployeeList: [],
        fileList: []
      },
      dangerMeetExtraDTO: {},
      rules: {
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.noticeType = ~~this.$route.query.noticeType
    document.title = '中建八局'
    if (this.$route.query.businessId) {
      this.form.id = this.$route.query.businessId
      this.getData()
    }
  },
  methods: {
    async getData() {
      this.loading = true
      let res = await Api.findListByMainId({}, { id: this.form.id })
      this.loading = false
      if (res.code === this.SUCCESS_CODE) {
        this.form = res.data
        if (this.form.status !== this.STATUS.DRAFT) {
          let res2 = await Api.getExtraList({}, { dangerMeetId: this.form.id })
          if (res2.code === this.SUCCESS_CODE) {
            this.extraList = res2.data
          }
        }
      }
    },
    save() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let temp = this.$clone(this.dangerMeetExtraDTO)
          temp.dangerMeetId = this.form.id
          switch (this.noticeType) {
            case 201:
              temp.noticType = 'TYPE_5'
              break
            case 202:
              temp.noticType = 'TYPE_6'
              break
            case 203:
              temp.noticType = 'TYPE_7'
              break
            case 204:
              temp.noticType = 'TYPE_8'
              break
          }
          temp.userId = this.$userInfo().id
          temp.userName = this.$userInfo().name

          this.loading = true
          let res = await Api.addExtra(temp)
          this.loading = false
          if (res.code === this.SUCCESS_CODE) {
            await finishBusinessTodo({
              id: this.$route.query.messageId
            })
            this.$noticeSuccess('操作成功')
            this.$close()
          }
        }
      })
    }
  }
}
</script>
<style scoped>
.page-btn-right-top  {
  top:5px
}
</style>